package xcmg.device.service.packageSpec;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import yb.ecp.fast.infra.util.PageHelperPlus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xcmg.device.dao.entity.packageSpec.PackageSpecDO;
import xcmg.device.dao.mapper.packageSpec.PackageSpecDOMapper;
import xcmg.device.dao.mapper.warehouse.WarehouseInventoryMapper;
import xcmg.device.infra.ErrorCode;
import xcmg.device.infra.enums.BillDesc;
import xcmg.device.service.GenDocumentService;
import yb.ecp.fast.feign.FastGenClient;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;
import yb.ecp.fast.infra.infra.log.LogHelper;

import java.util.List;

/**
 * @author ZHANGK
 * @date 2020/12/14
 **/
@Service
public class PackageSpecService {

    @Autowired
    PackageSpecDOMapper packageSpecDOMapper;

    @Autowired
    FastGenClient fastGenClient;

    @Autowired
    GenDocumentService genDocumentService;

    @Autowired
    WarehouseInventoryMapper warehouseInventoryMapper;

    @Value("${jck.orgId:0}")
    private String jkcOrgId;

    public PageCommonVO<PackageSpecDO> list(SearchCommonVO<PackageSpecDO> condition){
        PageHelperPlus.startPage(condition.getPageNum(), condition.getPageSize());
        List<PackageSpecDO> list = packageSpecDOMapper.list(condition.getFilters());
        PageCommonVO<PackageSpecDO> result = new PageCommonVO<>();
        result.setPageInfo(new PageInfo<>(list));
        return result;
    }

    @Transactional(rollbackFor = Exception.class)
    public ErrorCode add(PackageSpecDO packageSpecDO) {
        ActionResult<String> fastGenResult = fastGenClient.textGuid();
        packageSpecDO.setPackageNo(genDocumentService.getDocumentNo(BillDesc.PackageSpec));
        if (fastGenResult.getCode() != ErrorCode.Success.getCode()) {
            return ErrorCode.IllegalArument;
        }
        packageSpecDO.setId(fastGenResult.getValue());
        int result = packageSpecDOMapper.insert(packageSpecDO);
        if (result > 0) {
            return ErrorCode.Success;
        }
        return ErrorCode.Failure;
    }

    @Transactional(rollbackFor = Exception.class)
    public ErrorCode update(PackageSpecDO packageSpecDO){
        int result = packageSpecDOMapper.update(packageSpecDO);
        if (result > 0){
            return ErrorCode.Success;
        }
        return ErrorCode.Failure;
    }

    public PackageSpecDO item(String id,String orgId) {
        PackageSpecDO item = packageSpecDOMapper.item(id);
        LogHelper.debug("调用item获取数据,id:" + id + ", item:" + JSON.toJSONString(item));
        item.setQty(warehouseInventoryMapper.findEnableNum(item.getMaterialsNo(), jkcOrgId, null));
        return item;
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean updateInventoryQtyByPackageSpecId(String packageSpecId) {
        PackageSpecDO packageSpecDO = packageSpecDOMapper.item(packageSpecId);
        return warehouseInventoryMapper.deductInventoryByMaterialsNo(packageSpecDO.getMaterialsNo(), jkcOrgId);
        //return packageSpecDOMapper.updateInventoryQtyByPackageSpecId(packageSpecId);
    }

    public List<PackageSpecDO> listAll(String orgId) {
        PackageSpecDO packageSpecDO = new PackageSpecDO();
        //packageSpecDO.setOrgId(orgId);
        return packageSpecDOMapper.list(packageSpecDO);
    }
}
